#include "ses_timer.h"



// Timer registers
#define TIMER2_INT_vect  		TIMER2_COMPA_vect
#define TIMER2_MODE_A			1<< WGM21 	// | (1<< CMP...) alternatively
#define TIMER2_MODE_B			0x04
#define TIMER2_INT_MASK			1<<OCIE2A
#define TIMER2_INT_FLAG         			// TODO check if needed
#define TIMER2_CYC_FOR_1MILLISEC	250 	// triggers interrupt after 1 ms



/* Callback function is executed on interrupt. */
pTimerCallback callback;



void timer2_init(pTimerCallback cb) {
	TCNT2 = 0;
	TCCR2A = TIMER2_MODE_A;
	TCCR2B = TIMER2_MODE_B;
	OCR2A = TIMER2_CYC_FOR_1MILLISEC;
	TIMSK2 = TIMER2_INT_MASK;

	// The callback function will point to the function cb. Each time an
	// interrupt is triggered callback() will be executed.
	callback = cb;
}

/*
 * Triggered every 1 ms.
 */
ISR(TIMER2_INT_vect) {
	callback();
}
